<?php

/**
 * Description of PageModel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class PageModel extends Page {

    /**
     * The constructor.
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * Getting page parameters for passing into a view.
     *
     * @return array
     */
    public function getViewFields() {
/*
        $searchString = Request::getVariable('search_string');
        $domainId = Request::getVariable('domain_id');
        $subdomainId = Request::getVariable('subdomain_id');
        $pageId = Request::getVariable('page_id');
        $panelId = Request::getVariable('panel_id');
        $articleId = Request::getVariable('article_id');
        $htmlFormId = Request::getVariable('html_form_id');
        
        $viewFields = parent::getViewFields();
        $pageNumber = self::getCurrentPageNumber();
        $article = self::getCurrentPageArticle();
        $fileExtension = self::getCurrentPagePseudoFile();
        if (is_array($viewFields['page'])) {
            $name = $viewFields['page']['name'];
        }*/
        
        /**
         * Page specific data for Admin.
         */
        /*
        $data = NULL;
        switch ($name) {
            case 'domains':
                $data = DomainModel::getDomainData($searchString, $pageNumber);
                break;
            case 'subdomains':
                $data = SubdomainModel::getSubdomainData($domainId, $searchString, $pageNumber);
                break;
            case 'pages':
                $data = self::getPageData($subdomainId, $searchString, $pageNumber);
                break;
            case 'panels':
                $data = PanelModel::getPanelData($pageId, $searchString, $pageNumber);
                break;
            case 'articles':
                $data = ArticleModel::getArticleData($panelId, $searchString, $pageNumber);
                break;
            case 'forms':
                $data = HtmlFormModel::getHtmlFormData($articleId, $searchString, $pageNumber);
                break;
            case 'formelements':
                $data = HtmlFormElementModel::getHtmlFormElementData($htmlFormId, $searchString, $pageNumber);
                break;
        }
        
        if (is_array($viewFields['page'])) {
            $viewFields['page']['data'] = $data;
            $viewFields['page']['page_number'] = $pageNumber;
            $viewFields['page']['article'] = $article;
            $viewFields['page']['file_extension'] = $fileExtension;
        }
         * 
         */
        $viewFields = parent::getViewFields();
        $pageNumber = self::getCurrentPageNumber();
        $article = self::getCurrentPageArticle();
        $fileExtension = self::getCurrentPagePseudoFile();
        if (is_array($viewFields['page'])) {
            $name = $viewFields['page']['name'];
        }

        /**
         * Page specific data.
         */
        if (is_array($viewFields['page'])) {
            $viewFields['page']['page_number'] = $pageNumber;
            $viewFields['page']['article'] = $article;
            $viewFields['page']['file_extension'] = $fileExtension;
        }
        $viewFields['test'] = Registry::getFlds('MULTILINGUAL_SITE');
        return $viewFields;
    }

    /**
     * ADMIN SPECIFIC METHODS.
     */

    /**
     * Getting data for all pages within a given subdomain.
     * Returns paginated results in descending order.
     *
     * @param int $subdomainId
     * @param int $pageNumber
     * @return array
     */
    public static function getPageData($subdomainId, $searchString = NULL, $pageNumber = 1) {

        /**
         * Get paginated results.
         */
        $queryCount = new Query('
            SELECT COUNT(*) AS `count`
            FROM `page`
            :where
            :and :search;
        ');
        $queryCount = AdminUtils::queryProcessing($queryCount, 'page', 'subdomain', $subdomainId, $searchString);
        $query = new Query('
            SELECT :select
            FROM `page`
            :join
            :where
            :and :search
            ORDER BY `page`.`subdomain`, `page`.`id` DESC
            LIMIT :offset, :row_count;
        ');
        $query = AdminUtils::queryProcessing($query, 'page', 'subdomain', $subdomainId, $searchString);

        $pagination = new Pagination(
            $query,
            $queryCount,
            $pageNumber
        );
        $output = array();
        $output['tabulated'] = $pagination->getRows();
        $output['navigation_array'] = $pagination->getNavigationArray();

        /**
         * Get domain and subdomain data.
         */
        if ($subdomainId != NULL) {
            $subdomain = new Subdomain;
            $subdomain->load($subdomainId);
            $output['subdomain'] = $subdomain->getViewFields();
            $output['domain'] = $subdomain->getDomain()->getViewFields();
        }
        
        return $output;
    }

}